﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using HGZP.BLL;
using HGZP.Model;
using HGZP.PLL;

namespace Admin
{
    /// <summary>
    /// 功能：对权限操作
    /// 制件人：商伟
    /// 制作时间:2011-06-16
    /// </summary>
    public class Purview : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int type = Convert.ToInt16(context.Request.QueryString["type"].ToString());
            string id = context.Request.QueryString["roleID"];
            if (type == 1)
            {
                context.Response.Write(GetPurview(id));
            }
            else
            {
                string purlist = context.Request.QueryString["function"].ToString();
                context.Response.Write(SetPurview(id, purlist));
            }

        }

        /// <summary>
        /// 根据角色ID获取权限
        /// </summary>
        /// <param name="id">角色ID</param>
        /// <returns></returns>
        public string GetPurview(string id)
        {
            string strFunction = string.Empty;
            CBasePurviewBLL basePurview = new CBasePurviewBLL();
            DataTable dtPurivew = basePurview.F02_SelectHaveWhere(string.Format("{0}='{1}'",basePurview.sGuidBaseRole, id));
            if (dtPurivew.Rows.Count > 0)
            {
                for (int i = 0; i < dtPurivew.Rows.Count; i++)
                {
                    if (i == dtPurivew.Rows.Count - 1)
                    {
                        strFunction += dtPurivew.Rows[i][basePurview.sGuidInfomenu].ToString();
                    }
                    else
                    {
                        strFunction += dtPurivew.Rows[i][basePurview.sGuidInfomenu].ToString() + "|";
                    }
                }
            }
            return strFunction;

        }
        /// <summary>
        /// 设置角色权限
        /// </summary>
        /// <param name="id">角色ID</param>
        /// <param name="functionList">功能ID</param>
        /// <returns>成功或错误说明</returns>
        public string  SetPurview(string id, string functionList)
        {
            try
            {
                CBasePurviewBLL basePurivew = new CBasePurviewBLL();
                DataTable da=basePurivew.F02_SelectHaveWhere(string.Format("{0}='{1}'", basePurivew.sGuidBaseRole, id));
                basePurivew.myCBasePurviewCollection.F50_DeleteAll();
                string[] function = functionList.Split('|');
                for (int i = 0; i < function.Length; i++)
                {
                    CBasePurview basePurviewModel = new CBasePurview();
                    basePurviewModel.sGuid = MyFunction.GetNewGuid();
                    basePurviewModel.sGuidBaseRole = id;
                    basePurviewModel.sGuidInfomenu = function[i].ToString().Trim();
                    basePurivew.F30_Insert(basePurviewModel);
                }
                DBSaveBLL dbSave = new DBSaveBLL();
                dbSave.PrepareSave(basePurivew.myCBasePurviewCollection);
                bool bl=dbSave.Save();
                return "true";
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
            
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}